home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / opcodes / arm-dis.c < prev    next >
C/C++ Source or Header  |  1994-09-05  |  9KB  |  420 lines

  1. /* Instruction printing code for the ARM
  2.    Copyright (C) 1994 Free Software Foundation, Inc. 
  3.    Contributed by Richard Earnshaw (rwe@pegasus.esprit.ec.org)
  4.  
  5. This file is part of libopcodes. 
  6.  
  7. This program is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your option)
  10. any later version. 
  11.  
  12. This program is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  15. more details. 
  16.  
  17. You should have received a copy of the GNU General Public License along with
  18. This program; if not, write to the Free Software Foundation, Inc., 675
  19.  Mass Ave, Cambridge, MA 02139, USA.  
  20. */
  21.  
  22. #include "dis-asm.h"
  23. #define DEFINE_TABLE
  24. #include "arm-opc.h"
  25.  
  26.  
  27. static char *arm_conditional[] =
  28. {"eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
  29.  "hi", "ls", "ge", "lt", "gt", "le", "", "nv"};
  30.  
  31. static char *arm_regnames[] =
  32. {"r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  33.  "r8", "r9", "sl", "fp", "ip", "sp", "lr", "pc"};
  34.  
  35. static char *arm_fp_const[] =
  36. {"0.0", "1.0", "2.0", "3.0", "4.0", "5.0", "0.5", "10.0"};
  37.  
  38. static char *arm_shift[] = 
  39. {"lsl", "lsr", "asr", "ror"};
  40.  
  41. static void
  42. arm_decode_shift (given, func, stream)
  43.      long given;
  44.      fprintf_ftype func;
  45.      void *stream;
  46. {
  47.   func (stream, "%s", arm_regnames[given & 0xf]);
  48.   if ((given & 0xff0) != 0)
  49.     {
  50.       if ((given & 0x10) == 0)
  51.     {
  52.       int amount = (given & 0xf80) >> 7;
  53.       int shift = (given & 0x60) >> 5;
  54.       if (amount == 0)
  55.         {
  56.           if (shift == 3)
  57.         {
  58.           func (stream, ", rrx");
  59.           return;
  60.         }
  61.           amount = 32;
  62.         }
  63.       func (stream, ", %s #%x", arm_shift[shift], amount);
  64.     }
  65.       else
  66.     func (stream, ", %s %s", arm_shift[(given & 0x60) >> 5],
  67.           arm_regnames[(given & 0xf00) >> 8]);
  68.     }
  69. }
  70.  
  71. /* Print one instruction from PC on INFO->STREAM.
  72.    Return the size of the instruction (always 4 on ARM). */
  73.  
  74. int
  75. print_insn_arm (pc, info)
  76.     bfd_vma         pc;
  77.     struct disassemble_info *info;
  78. {
  79.   struct arm_opcode *insn;
  80.   unsigned char b[4];
  81.   void *stream = info->stream;
  82.   fprintf_ftype func = info->fprintf_func;
  83.   int status;
  84.   long given;
  85.  
  86.   status = (*info->read_memory_func) (pc, (bfd_byte *) &b[0], 4, info);
  87.   if (status != 0) {
  88.     (*info->memory_error_func) (status, pc, info);
  89.     return -1;
  90.   }
  91.   given = (b[0]) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24);
  92.  
  93.   func (stream, "%08x\t", given);
  94.  
  95.   for (insn = arm_opcodes; insn->assembler; insn++)
  96.     {
  97.       if ((given & insn->mask) == insn->value)
  98.     {
  99.       char *c;
  100.       for (c = insn->assembler; *c; c++)
  101.         {
  102.           if (*c == '%')
  103.         {
  104.           switch (*++c)
  105.             {
  106.             case '%':
  107.               func (stream, "%%");
  108.               break;
  109.  
  110.             case 'a':
  111.               if (((given & 0x000f0000) == 0x000f0000)
  112.               && ((given & 0x02000000) == 0))
  113.             {
  114.               int offset = given & 0xfff;
  115.               if ((given & 0x00800000) == 0)
  116.                 offset = -offset;
  117.               (*info->print_address_func)
  118.                 (offset + pc + 8, info);
  119.             }
  120.               else
  121.             {
  122.               func (stream, "[%s", 
  123.                 arm_regnames[(given >> 16) & 0xf]);
  124.               if ((given & 0x01000000) != 0)
  125.                 {
  126.                   if ((given & 0x02000000) == 0)
  127.                 {
  128.                   int offset = given & 0xfff;
  129.                   if (offset)
  130.                     func (stream, ", %s#%x",
  131.                       (((given & 0x00800000) == 0)
  132.                        ? "-" : ""), offset);
  133.                 }
  134.                   else
  135.                 {
  136.                   func (stream, ", %s",
  137.                     (((given & 0x00800000) == 0)
  138.                      ? "-" : ""));
  139.                   arm_decode_shift (given, func, stream);
  140.                 }
  141.  
  142.                   func (stream, "]%s", 
  143.                     ((given & 0x00200000) != 0) ? "!" : "");
  144.                 }
  145.               else
  146.                 {
  147.                   if ((given & 0x02000000) == 0)
  148.                 {
  149.                   int offset = given & 0xfff;
  150.                   if (offset)
  151.                     func (stream, "], %s#%x",
  152.                       (((given & 0x00800000) == 0)
  153.                        ? "-" : ""), offset);
  154.                   else 
  155.                     func (stream, "]");
  156.                 }
  157.                   else
  158.                 {
  159.                   func (stream, "], %s",
  160.                     (((given & 0x00800000) == 0) 
  161.                      ? "-" : ""));
  162.                   arm_decode_shift (given, func, stream);
  163.                 }
  164.                 }
  165.             }
  166.               break;
  167.               
  168.             case 'b':
  169.               (*info->print_address_func)
  170.             (BDISP (given) * 4 + pc + 8, info);
  171.               break;
  172.  
  173.             case 'c':
  174.               func (stream, "%s",
  175.                 arm_conditional [(given >> 28) & 0xf]);
  176.               break;
  177.  
  178.             case 'm':
  179.               {
  180.             int started = 0;
  181.             int reg;
  182.  
  183.             func (stream, "{");
  184.             for (reg = 0; reg < 16; reg++)
  185.               if ((given & (1 << reg)) != 0)
  186.                 {
  187.                   if (started)
  188.                 func (stream, ", ");
  189.                   started = 1;
  190.                   func (stream, "%s", arm_regnames[reg]);
  191.                 }
  192.             func (stream, "}");
  193.               }
  194.               break;
  195.  
  196.             case 'o':
  197.               if ((given & 0x02000000) != 0)
  198.             {
  199.               int rotate = (given & 0xf00) >> 7;
  200.               int immed = (given & 0xff);
  201.               func (stream, "#%x",
  202.                 ((immed << (32 - rotate))
  203.                  | (immed >> rotate)) & 0xffffffff);
  204.             }
  205.               else
  206.             arm_decode_shift (given, func, stream);
  207.               break;
  208.  
  209.             case 'p':
  210.               if ((given & 0x0000f000) == 0x0000f000)
  211.             func (stream, "p");
  212.               break;
  213.  
  214.             case 't':
  215.               if ((given & 0x01200000) == 0x00200000)
  216.             func (stream, "t");
  217.               break;
  218.  
  219.             case 'A':
  220.               func (stream, "[%s", arm_regnames [(given >> 16) & 0xf]);
  221.               if ((given & 0x01000000) != 0)
  222.             {
  223.               int offset = given & 0xff;
  224.               if (offset)
  225.                 func (stream, ", %s#%x]%s",
  226.                   ((given & 0x00800000) == 0 ? "-" : ""),
  227.                   offset * 4,
  228.                   ((given & 0x00200000) != 0 ? "!" : ""));
  229.               else
  230.                 func (stream, "]");
  231.             }
  232.               else
  233.             {
  234.               int offset = given & 0xff;
  235.               if (offset)
  236.                 func (stream, "], %s#%x",
  237.                   ((given & 0x00800000) == 0 ? "-" : ""),
  238.                   offset * 4);
  239.               else
  240.                 func (stream, "]");
  241.             }
  242.               break;
  243.  
  244.             case 'C':
  245.               switch (given & 0x00090000)
  246.             {
  247.             case 0:
  248.               func (stream, "_???");
  249.               break;
  250.             case 0x10000:
  251.               func (stream, "_ctl");
  252.               break;
  253.             case 0x80000:
  254.               func (stream, "_flg");
  255.               break;
  256.             }
  257.               break;
  258.  
  259.             case 'F':
  260.               switch (given & 0x00408000)
  261.             {
  262.             case 0:
  263.               func (stream, "4");
  264.               break;
  265.             case 0x8000:
  266.               func (stream, "1");
  267.               break;
  268.             case 0x00400000:
  269.               func (stream, "2");
  270.               break;
  271.             default:
  272.               func (stream, "3");
  273.             }
  274.               break;
  275.             
  276.             case 'P':
  277.               switch (given & 0x00080080)
  278.             {
  279.             case 0:
  280.               func (stream, "s");
  281.               break;
  282.             case 0x80:
  283.               func (stream, "d");
  284.               break;
  285.             case 0x00080000:
  286.               func (stream, "e");
  287.               break;
  288.             default:
  289.               func (stream, "<illegal precision>");
  290.               break;
  291.             }
  292.               break;
  293.             case 'Q':
  294.               switch (given & 0x00408000)
  295.             {
  296.             case 0:
  297.               func (stream, "s");
  298.               break;
  299.             case 0x8000:
  300.               func (stream, "d");
  301.               break;
  302.             case 0x00400000:
  303.               func (stream, "e");
  304.               break;
  305.             default:
  306.               func (stream, "p");
  307.               break;
  308.             }
  309.               break;
  310.             case 'R':
  311.               switch (given & 0x60)
  312.             {
  313.             case 0:
  314.               break;
  315.             case 0x20:
  316.               func (stream, "p");
  317.               break;
  318.             case 0x40:
  319.               func (stream, "m");
  320.               break;
  321.             default:
  322.               func (stream, "z");
  323.               break;
  324.             }
  325.               break;
  326.  
  327.             case '0': case '1': case '2': case '3': case '4': 
  328.             case '5': case '6': case '7': case '8': case '9':
  329.               {
  330.             int bitstart = *c++ - '0';
  331.             int bitend = 0;
  332.             while (*c >= '0' && *c <= '9')
  333.               bitstart = (bitstart * 10) + *c++ - '0';
  334.  
  335.             switch (*c)
  336.               {
  337.               case '-':
  338.                 c++;
  339.                 while (*c >= '0' && *c <= '9')
  340.                   bitend = (bitend * 10) + *c++ - '0';
  341.                 if (!bitend)
  342.                   abort ();
  343.                 switch (*c)
  344.                   {
  345.                   case 'r':
  346.                 {
  347.                   long reg;
  348.                   reg = given >> bitstart;
  349.                   reg &= (2 << (bitend - bitstart)) - 1;
  350.                   func (stream, "%s", arm_regnames[reg]);
  351.                 }
  352.                 break;
  353.                   case 'd':
  354.                 {
  355.                   long reg;
  356.                   reg = given >> bitstart;
  357.                   reg &= (2 << (bitend - bitstart)) - 1;
  358.                   func (stream, "%d", reg);
  359.                 }
  360.                 break;
  361.                   case 'x':
  362.                 {
  363.                   long reg;
  364.                   reg = given >> bitstart;
  365.                   reg &= (2 << (bitend - bitstart)) - 1;
  366.                   func (stream, "0x%08x", reg);
  367.                 }
  368.                 break;
  369.                   case 'f':
  370.                 {
  371.                   long reg;
  372.                   reg = given >> bitstart;
  373.                   reg &= (2 << (bitend - bitstart)) - 1;
  374.                   if (reg > 7)
  375.                     func (stream, "#%s",
  376.                       arm_fp_const[reg & 7]);
  377.                   else
  378.                     func (stream, "f%d", reg);
  379.                 }
  380.                 break;
  381.                   default:
  382.                 abort ();
  383.                   }
  384.                 break;
  385.               case '`':
  386.                 c++;
  387.                 if ((given & (1 << bitstart)) == 0)
  388.                   func (stream, "%c", *c);
  389.                 break;
  390.               case '\'':
  391.                 c++;
  392.                 if ((given & (1 << bitstart)) != 0)
  393.                   func (stream, "%c", *c);
  394.                 break;
  395.               case '?':
  396.                 ++c;
  397.                 if ((given & (1 << bitstart)) != 0)
  398.                   func (stream, "%c", *c++);
  399.                 else
  400.                   func (stream, "%c", *++c);
  401.                 break;
  402.               default:
  403.                 abort ();
  404.               }
  405.             break;
  406.  
  407.               default:
  408.             abort ();
  409.               }
  410.             }
  411.         }
  412.           else
  413.         func (stream, "%c", *c);
  414.         }
  415.       return 4;
  416.     }
  417.     }
  418.   abort ();
  419. }
  420.